package com.joplus.housekeeper.ui.view.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.joplus.housekeeper.R;
import com.joplus.housekeeper.ui.view.CustomShapeImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by ccm on 2016/7/5 14:14
 */
public class ServiceSelectDialog extends PopupWindow {


    private View mMenuView;
    private Context mContext;

    private CustomShapeImageView iv;
    private final ListView lv;

    List<String> data;

    public ServiceSelectDialog(Context context, List<String> list) {
        super(context);
        mContext = context;
        this.data = list;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.dialog_service_list, null);


        lv = (ListView) mMenuView.findViewById(R.id.lv);

        //设置按钮监听

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
     /*   mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.share_ll_menu).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        //  dismiss();
                    }
                }
                return true;
            }
        });*/
        lv.setAdapter(new ArrayAdapter<String>(context, R.layout.item_rv_text, data));
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (payClick != null) {
                    payClick.onclick(data.get(position));
                }
            }
        });


        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackground(1);
            }
        });
    }

    private boolean bao = false;

    //mb的包包币
    public void setType() {
        bao = true;
    }


    private Activity activity;

    public void show(Activity activity) {
        this.activity = activity;
        showAtLocation(activity.getWindow().getDecorView(), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        setBackground(0.5f);
    }

    public void setIv(String url) {
        ImageLoader.getInstance().displayImage(url, iv);
    }

    private PayClick payClick;


    public void setOnPayClickListener(PayClick payClick) {
        this.payClick = payClick;
    }

    public interface PayClick {
        void onclick(String string);
    }


    private void setBackground(float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        if (bgAlpha == 1) {
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        activity.getWindow().setAttributes(lp);
    }
}
